package com.sanyang.lawyer.controller.Case_law;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.ResultApi.CodeMsg;
import com.sanyang.lawyer.ResultApi.ResponseResult;
import com.sanyang.lawyer.entity.Case_law;
import com.sanyang.lawyer.entity.Case_message;
import com.sanyang.lawyer.service.Case.Case_lawService;
import com.sanyang.lawyer.utils.ActivitiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("law")
public class LawController {
    
    @Autowired
    private Case_lawService lawService;
    
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private ActivitiUtils activitiUtils;
    

    @RequestMapping("selectLikeList")
    public ResponseResult<List<Case_law>> selectLikeList(HttpServletRequest request, String data){ //模糊查询所有
        Case_law case_law = null;
        if (data!=null){
            try {
                case_law = objectMapper.readValue(data, Case_law.class);
            } catch (IOException e) {
                ResponseResult.error(CodeMsg.SERVER_BUSY);
            }
        }
        return ResponseResult.success(lawService.selectLikeList(case_law));
    }

    //根据id查询单条
    @RequestMapping("queryByid")
    public ResponseResult<Case_law> queryByid(String caseid){
        return ResponseResult.success(lawService.queryByid(caseid));
    }
    
    //利益冲突监测
    @RequestMapping("detection")
    public ResponseResult<List<Case_law>> detection(String data){
        Case_message case_message = null;
        try {
            case_message = objectMapper.readValue(data, Case_message.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        return ResponseResult.success(lawService.detection(case_message));
    }

    //查询最大的version
    @RequestMapping("getMaxKey")
    public ResponseResult<Integer> getMaxKey(){
        return ResponseResult.success(lawService.getMaxKey());
    }

    //添加一条案件
    @RequestMapping("insertOne")
    public ResponseResult<String> insertOne(String data){
        Case_law case_law = null;
        try {
            case_law = objectMapper.readValue(data, Case_law.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        
        case_law.setCaseStatus("未结案");
        case_law.setSubmitDate(new Date());
        case_law.setCloseMode("未收案");
        case_law.setCloseState("审批中");  
        //应付金额等于代理费用加杂费
        case_law.setAmountMoney(case_law.getAgencyMoney() + case_law.getBlendMoney());
        //初始化添加未付金额等于代理费用加杂费
        case_law.setCase_unpaidsal(case_law.getAgencyMoney() + case_law.getBlendMoney());
        
        lawService.insertOne(case_law);
        return ResponseResult.success("成功添加！~");
    }
    
    //根据案件id查询案号,委托人名称，代理费用，杂费，未付金额，应付金额，
    @RequestMapping("gatheringQuery")
    public ResponseResult<Case_law> gatheringQuery(String caseid){
        return ResponseResult.success(lawService.gatheringQuery(caseid));
    }
    
    //修改数据库中的数据  修改未付,实付.
    @RequestMapping("gathering")
    public ResponseResult<String> gathering(String data){
        Case_law case_law = null;
        try {
            case_law = objectMapper.readValue(data, Case_law.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        lawService.gathering(case_law);
        return ResponseResult.success("成功");
    }
    
    
    //案件修改
    @RequestMapping("Case_update")
    public ResponseResult<String> Case_update(String data){
        Case_law case_law = null;
        try {
            case_law = objectMapper.readValue(data, Case_law.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        System.out.println(case_law);
        lawService.Case_update(case_law);
        return ResponseResult.success("成功修改！~");
    }

    //查询出所有的已删除的案件
    @RequestMapping("selectCaseBystatus")
    public ResponseResult<List<Case_law>> selectCaseBystatus(String data){ 
        Case_law case_law = null;
        if (data!=null){
            try {
                case_law = objectMapper.readValue(data, Case_law.class);
            } catch (IOException e) {
                ResponseResult.error(CodeMsg.SERVER_BUSY);
            }
        }
        return ResponseResult.success(lawService.selectCaseBystatus(case_law));
    }
       
    //根据案件id将案件表的状态修改为已删除
    @RequestMapping("deleteCase")
    public ResponseResult<String> deleteCase(String caseid){
        lawService.deleteCase(caseid);
        return ResponseResult.success("成功删除！~");
    } 
    
    
    
}
